﻿/*
 * Copyright (c) 2011 Shawn Rakowski
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to 
 * deal in the Software without restriction, including without limitation the 
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 
 * sell copies of the Software, and to permit persons to whom the Software is 
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in 
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
 * SOFTWARE. 
 * 
 */

using System.Collections.Generic;
using ROS.Application.DomainObjects;
using ROS.Persistence.DAO.DTO;
using ROS.Persistence.DAO;

namespace ROS.Persistence
{
    internal class UserDataMapper : IDataMapper<User>
    {
        #region Fields

        private IUserDAO _userDAO;

        #endregion

        #region Properties

        /// <summary>
        /// Get the data access object we'll be using for persistence.
        /// </summary>
        private IUserDAO userDAO
        {
            get
            {
                if (_userDAO == null)
                {
                    DAOFactory daoFactory = DAOFactory.GetDAOFactory("MySQL");
                    _userDAO = daoFactory.GetUserDAO();
                }
                return _userDAO;
            }
        }

        #endregion

        #region Constructor

        /// <summary>
        /// Construct a new UserDataMapper object.
        /// </summary>
        public UserDataMapper()        
        {
            _userDAO = null;
        }

        #endregion

        #region Methods

        /// <summary>
        /// Create a new User storage.
        /// </summary>
        /// <param name="user"></param>
        public void Create(User user)
        {
            UserDTO userDTO = mapUserToDTO(user);
            userDAO.Insert(userDTO);
            user.ID = userDTO.ID;
        }

        /// <summary>
        /// Find a user by their User ID.
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        public User FindByID(long userID)
        {
            UserDTO userDTO = userDAO.SelectByID(userID);
            return mapDTOToUser(userDTO);
        }

        /// <summary>
        /// Find all Users in storage.
        /// </summary>
        /// <returns></returns>
        public IList<User> FindAll()
        {
            IList<User> userList = new List<User>();
            IList<UserDTO> userDTOList = userDAO.SelectAll();
            foreach (UserDTO userDTO in userDTOList)
                userList.Add(mapDTOToUser(userDTO));
            return userList;
        }

        /// <summary>
        /// Update a user in storage.
        /// </summary>
        /// <param name="user"></param>
        public void Update(User user)
        {
            UserDTO userDTO = mapUserToDTO(user);
            userDAO.Update(userDTO);
        }

        /// <summary>
        /// Delete a User from storage.
        /// </summary>
        /// <param name="user"></param>
        public void Delete(User user)
        {
            userDAO.DeleteByID(user.ID);
        }

        /// <summary>
        /// Constructs a data transfer object from a User object to send
        /// to the data source.
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        private UserDTO mapUserToDTO(User user)
        {
            UserDTO userDTO = new UserDTO();
            userDTO.ID = user.ID;
            userDTO.FirstName = user.FirstName;
            userDTO.LastName = user.LastName;
            userDTO.IsActive = user.IsActive;
            userDTO.UserType = (int)user.UserType;
            return userDTO;
        }

        /// <summary>
        /// Constructs a User domain object from the data returned
        /// from the data source.
        /// </summary>
        /// <param name="userDTO"></param>
        /// <returns></returns>
        private User mapDTOToUser(UserDTO userDTO)
        {
            return new User(userDTO.ID,
                            userDTO.IsActive,
                            userDTO.FirstName,
                            userDTO.LastName,
                            (UserType)userDTO.UserType);
        }

        #endregion
    }
}
